﻿using HN.WxPayApiV3Partner.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HN.WxPayApiV3Partner
{
    /// <summary>
    /// 请先配置常量=》 V3Const.cs
    /// 该demo为微信支付》服务商平台》V3版本》JSAPI支付
    /// author:sushuai
    /// WxValue:suxiaoshuai1201
    /// telphone:17865850678
    /// qq:867888799
    /// </summary>
    internal class Program
    {
        /// <summary>
        /// 测试商户的openid 商户的，非服务商的
        /// </summary>
        private static string openid = "imopenid";
        /// <summary>
        /// 测试订单金额 这里设置为2毛钱
        /// </summary>
        private static decimal order_amount = (decimal)0.2;
        /// <summary>
        /// 测试订单退款金额 这里设置为1毛钱
        /// </summary>
        private static decimal order_refund_amount = (decimal)0.1;
        static void Main(string[] args)
        {
            //JSAPI下单();
            //微信支付通知地址数据解析();
            //订单查询("4200001311202204182369347778");
            //申请退款API("4200001311202204182369347778");
            //查询单笔退款();
            //退款通知通知地址数据解析();

        }
        /// <summary>
        /// https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_1_1.shtml
        /// https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_1_4.shtml
        /// 1.获取prepay_id
        /// 2.获取H5调起JS API参数
        /// </summary>
        private static void JSAPI下单()
        {
            //统一下单
            V3ParamtData data = new V3ParamtData();
            data.SetValue("sp_appid", V3Const.sp_appid);
            data.SetValue("sp_mchid", V3Const.sp_mchid);
            data.SetValue("sub_appid", V3Const.sub_appid);
            data.SetValue("sub_mchid", V3Const.sub_mchid);
            data.SetValue("description", "我就是个描述");

            string out_trade_no = "B" + V3Utils.GetOrderNumber();
            data.SetValue("out_trade_no", out_trade_no);
            V3Utils.AppLog("out_trade_no=" + out_trade_no);

            data.SetValue("time_expire", DateTime.Now.AddMinutes(110).ToString("yyyy-MM-ddTHH:mm:sszzz"));
            data.SetValue("notify_url", V3Const.notify_url);
            data.SetValue("amount", new { total = Convert.ToDouble(order_amount) * 100 });
            data.SetValue("payer", new { sub_openid = openid });
            string json = data.ToJson();
            string jsonString = V3Request.HttpRequest(V3Const.url_add_order, json.ToString());
            dynamic res = JsonConvert.DeserializeObject(jsonString);
            string prepay_id = res.prepay_id.ToString();
            Console.Write("prepay_id:" + prepay_id);
            V3ParamtNoSortData nodata = new V3ParamtNoSortData();
            nodata.SetValue("appId", V3Const.sub_appid);
            nodata.SetValue("timeStamp", V3Utils.GenerateTimeStamp());
            nodata.SetValue("nonceStr", V3Utils.GenerateNonceStr());
            nodata.SetValue("package", "prepay_id=" + prepay_id);
            string sign = nodata.MakeSignJSAPI();
            nodata.SetValue("signType", "RSA");
            nodata.SetValue("paySign", sign);
            Console.Write("wxJsApiParam:" + nodata.ToJson());
        }
        /// <summary>
        /// https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_1_5.shtml
        /// </summary>
        private static void 微信支付通知地址数据解析()
        {
            //获取到的信息 请替换成自己滴
            string builder = "{\"id\":\"f7722f89-8efb-5e9b-b607-0e70af2c51a5\",\"create_time\":\"2022-04-18T21:40:23+08:00\",\"resource_type\":\"encrypt-resource\",\"event_type\":\"TRANSACTION.SUCCESS\",\"summary\":\"支付成功\",\"resource\":{\"original_type\":\"transaction\",\"algorithm\":\"AEAD_AES_256_GCM\",\"ciphertext\":\"DW4TCgfD/YmheZmRGnTC/8N973Cptq98Iw3Pbr+eE/HgtXtdC64gvSczt5H9WaoaH7fu+vfT6/JTp4EqSJAmk5JFTw56ER4aNZ9Wq970WPzg+sveByDiK1OJFIDIvG2/ARlvSxSpXVebaA1cnVWog0MEUYdz6xrI40k+b+71NCiVDw6Kh6RQZ//xiWW0PhcLJoBtgOT41HopF9ONvO26kT72US5cHZ1fppxV/IhhKTLzHM2I1/ZIX6cE7B0IuXiYN9VxI466twcP3hoNPsZgMulrocxQzyJ5WMgrfGYPuVVduLVpln6UbpuT+N9n4Ev6wq0QnLQTPkJWnDZwqoMhMifvvvGKo55CdVTV/hlaf7uGjdEd6MJ3jCLp8sUyry1JK7kluAzY7xhahbWeWoyadkxSO/gTrZHQ9Gy9PS4mTU4I1mjEMiuo/h11quuHyRCMqrd4Uw1hKsF//e6Se94R3WYvuemDFZX6WNxo0gX5wiXPhswjIVxXRneOQrwxATCmcdm+cO9PSX49XC+EwyaGOs3lJQOSN/P3gVYDnR044yWLUsDT+NwXsomhZb0pme2JMLrgi4SYZXQAAFUJJS1LwWmowpgLn8XP5LIi+wl4oLW22nGHUlERSue2+QgE3Xrsf+/n3s8AlVDWPcgpKTDorM9dDjjzBs9QeRxt+iP+6BstZqoMYjdUzOVyBXsAOHkTVq8qOSk0Qucd7+onjogywBARWvPHP7hP\",\"associated_data\":\"transaction\",\"nonce\":\"nPA6NXPi13tA\"}}";
            V3NotifyModel notifyUrlModel = JsonConvert.DeserializeObject<V3NotifyModel>(builder);
            //解密后的值
            string DecryptnotifyUrlModel = V3Utils.AesGcmDecrypt(notifyUrlModel);
            Console.Write(DecryptnotifyUrlModel);
        }
        /// <summary>
        /// https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_1_2.shtml
        /// </summary>
        /// <param name="transaction_id">微信支付交易号</param>
        private static void 订单查询(string transaction_id)
        {
            Dictionary<string, object> dicQuery = new Dictionary<string, object>();
            dicQuery.Add("sp_mchid", V3Const.sp_mchid);
            dicQuery.Add("sub_mchid", V3Const.sub_mchid);
            string result = V3Request.HttpRequest(V3Const.url_search_order, "", dicQuery, transaction_id);
            Console.Write("result:" + result);
            //{"amount":{"currency":"CNY","payer_currency":"CNY","payer_total":12,"total":12},"attach":"","bank_type":"CMB_CREDIT","out_trade_no":"B2204182116299855087","payer":{"sp_openid":"onLrj5g7ToeG3mPxWzGUL5xK1n-c","sub_openid":"oMZ7Zsoc91O_kHNYP33lrvd5sFrA"},"promotion_detail":[],"sp_appid":"wx95583d67d57ca2c5","sp_mchid":"1621403186","sub_appid":"wx0031db2694d850c6","sub_mchid":"1624649240","success_time":"2022-04-18T21:40:23+08:00","trade_state":"SUCCESS","trade_state_desc":"支付成功","trade_type":"JSAPI","transaction_id":"4200001311202204182369347778"}
        }
        /// <summary>
        /// https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_1_9.shtml
        /// </summary>
        private static void 申请退款API(string transaction_id, int refundOrderType = 0)
        {
            V3ParamtData data = new V3ParamtData();
            data.SetValue("sub_mchid", V3Const.sub_mchid);
            if (refundOrderType == 0)
                data.SetValue("transaction_id", transaction_id);
            else
                data.SetValue("out_trade_no", transaction_id);
            data.SetValue("out_refund_no", "T" + transaction_id);
            data.SetValue("notify_url", V3Const.notify_refunds_url);
            data.SetValue("amount", new
            {
                refund = Convert.ToDouble(order_refund_amount) * 100,
                total = Convert.ToDouble(order_amount) * 100,
                currency = "CNY"
            });
            string bodyString = data.ToJson();
            string jsonString = V3Request.HttpRequest(V3Const.url_add_order, bodyString);
            Console.Write(jsonString);
        }
        /// <summary>
        /// https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_1_10.shtml
        /// </summary>
        private static void 查询单笔退款()
        {
            //类似订单查询
        }
        /// <summary>
        /// https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter4_1_11.shtml
        /// </summary>
        private static void 退款通知通知地址数据解析()
        {
            //类似微信支付通知地址数据解析
        }
    }
}
